package com.ruoyi.ahs.util;

import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@Slf4j
public class WeChatBotMessageSender {

    private static String WEBHOOK_KEY = "";
    private static final String WEBHOOK_URL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=";

    private static Integer COUNT = 1;

    public static String getWebhookKey() {
        return WEBHOOK_KEY;
    }

    public static void setWebhookKey(String webhookKey) {
        WEBHOOK_KEY = webhookKey;
    }

    public static void setCount(int count) {
        COUNT = count;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        WeChatBotMessageSender.sendMessage("获取ssssssss", 5);
    }

    public static void sendMessage(String messageValue, Integer count) {
        int num = count == null ? COUNT : count;
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
            for (int i = 0; i < num; i++) {
                try {
                    Thread.sleep(1000);
                    WeChatBotMessageSender.sendMessage(messageValue);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public static void sendMessage(String messageValue) {
        try {
            // 创建一个URL对象
            URL url = new URL(WEBHOOK_URL + WeChatBotMessageSender.getWebhookKey());

            // 打开一个连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            // 设置为POST请求
            connection.setRequestMethod("POST");

            // 设置请求头，指定内容类型为JSON
            connection.setRequestProperty("Content-Type", "application/json; utf-8");
            connection.setRequestProperty("Accept", "application/json");

            // 设置请求超时
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);
            connection.setDoOutput(true);

            // 准备JSON格式的消息体
            JSONObject message = new JSONObject();
            message.put("msgtype", "text");

            JSONObject text = new JSONObject();
            text.put("content", messageValue);

            message.put("text", text);

            // 将JSON消息转换为字符串
            String jsonInputString = message.toString();

            // 发送POST请求
            try (OutputStream os = connection.getOutputStream()) {
                byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
                os.write(input, 0, input.length);
            }

            // 获取响应代码
            int responseCode = connection.getResponseCode();
            log.info("Response Code : {}", responseCode);
            // 关闭连接
            connection.disconnect();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
